<!doctype html>

<html lang="en">
<head>
	<title>Thread Demo - toxiclibs.js examples</title>
	<meta name="author" content="Kyle Phillips">
	<!-- Date: 2011-01-09 -->
	<script type="text/javascript" src="../build/toxiclibs.js"></script>
	<script type="text/javascript" src="js/processing.js"></script>
	<link href="css/style.css" rel="stylesheet" type="text/css" media="screen">
</head>
<body class="container_12">
	<header class="grid_12">
		<hgroup>
			<h1>Thread Demo</h1><h2>w/ <a href="http://processingjs.org">Processing.js</a>
		</hgroup>
	</header>

<script type="text/processing" data-processing-target="myCanvas">
/**
 *
 * @author Karsten Schmidt <info at postspectacular dot com>
 */

/* 
 * Copyright (c) 2008-2009 Karsten Schmidt
 * 
 * This demo & library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * http://creativecommons.org/licenses/LGPL/2.1/
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


 var  VerletPhysics2D = toxi.physics2d.VerletPhysics2D,
      VerletParticle2D = toxi.physics2d.VerletParticle2D,
      Vec2D = toxi.geom.Vec2D,
      ParticleString2D = toxi.physics2d.ParticleString2D;

import processing.opengl.*;

import toxi.physics2d.constraints.*;
import toxi.physics2d.*;

import toxi.geom.*;
import toxi.math.*;

int NUM_PARTICLES = 75;
int REST_LENGTH=10;

VerletPhysics2D physics;
VerletParticle2D head,tail;

boolean isTailLocked;

void setup() {
  size(960,720);
  smooth();
  physics=new VerletPhysics2D();
  Vec2D stepDir=new Vec2D(1,1).normalizeTo(REST_LENGTH);
  ParticleString2D s=new ParticleString2D(physics, new Vec2D(), stepDir, NUM_PARTICLES, 1, 0.1);
  head=s.getHead();
  head.lock();
  tail=s.getTail();
}

void draw() {
  background(0);
  stroke(255,100);
  noFill();
  head.set(mouseX,mouseY);
  physics.update();
  beginShape();
  int partLen = physics.particles.length;
  int i =0;
  for(i=0;i<partLen;i++) {
    VerletParticle2D p= physics.particles[i];
    vertex(p.x,p.y);
  }
  endShape();
  for(i=0;i<partLen;i++) {
    VerletParticle2D p= physics.particles[i];
    ellipse(p.x,p.y,5,5);
  }
}

void mousePressed() {
  isTailLocked=!isTailLocked;
  if (isTailLocked) {
    tail.lock();
  } 
  else {
    tail.unlock();
  }
}


</script>

<canvas id="myCanvas" width="200" height="200">
	your browser doesnt support canvas
</canvas>
<section class="grid_12" id="details">
<aside id="authors" class="grid_6">
<p>original example by <a href="http://postspectacular.com">Karsten Schmidt</a><br/>
	Toxiclibs ported to js by <a href="http://workofkylephillips.com">Kyle Phillips</a></p>
</aside>
<aside id="usage" class="grid_6">
	 <p><strong>Usage:</strong><br><ul>
	 <li>Click the mouse to lock/unlock the end of the string at its current position.</li>
	 <li>The head of the string is always linked to the current mouse position</li>
	</ul></p>
</aside>
<article id="description" class="grid_12">
<p> Thread demo showing the following:
 <ul><li>construction of a 2D string made from particles and springs/sticks using the toxi.physics2d.ParticleString2D class </li>
 	<li>dynamic locking & unlocking of particles</li>
 </ul>
 </p>
</p>
 </article>
</section>
</body>
</html>
